<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SearchController extends Controller
{
    //
    public function index(){
        $url='https://top.baidu.com/board?tab=realtime';
        $data=$this->sendDateByGet($url);
        preg_match('/<div style="margin-bottom:20px">[\s\S]*?<div class="container footer_aoJsU">/',$data,$match_data);
        preg_match_all('/<div class="category-wrap_iQLoo horizontal_1eKyQ">[\s\S]*?<!--\/s-frag-->/',$match_data[0],$match_data1);
        $arr=array();
        foreach ($match_data1[0] as $k=>$v){
            preg_match('/<div class="c-single-text-ellipsis">[\s\S]*?<\/div>/',$v,$title);
            preg_match('/<div class="index_1Ew5p[\s\S]*?<\/div>/',$v,$index);
            preg_match('/<div class="hot-index_1Bl1a[\s\S]*?<\/div>/',$v,$index_number);
            $arr[$k]=[
              'title'=>trim(strip_tags($title[0])),
              'index'=>trim(strip_tags($index[0])),
              'index_number'=>trim(strip_tags($index_number[0])),
              'time'=>time()
            ];
        }
        DB::table('bd_search')->insert($arr);
    }
    public function xl_search(){
        $url='https://s.weibo.com/top/summary?cate=realtimehot';
        $data=$this->sendDateByGet($url,true,'get','SUB=_2AkMX8nWgf8NxqwJRmf8Sz2Pqaol2zgzEieKhroR7JRMxHRl-yT9jqk84tRB6PHJbTxl1hCVkkNDaG2WMvQhpAxS6ZsN2; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9Wh_uTzbIeyKC2BDKqqN..4S; SINAGLOBAL=2444958750572.932.1622080182606; UOR=www.baidu.com,weibo.com,www.baidu.com; _s_tentry=www.baidu.com; Apache=8047537129538.969.1635217808140; ULV=1635217808177:9:2:1:8047537129538.969.1635217808140:1633278846408');
        preg_match('/<tbody>[\s\S]*?<\/tbody>/',$data,$match_data);
        preg_match_all('/<tr class="">[\s\S]*?<\/tr>/',$match_data[0],$match_data1);
//        dump($match_data1);
        $arr=array();
        foreach ($match_data1[0] as $k=>$v){
            preg_match('/<td class="td-02">[\s\S]*?<\/td>/',$v,$title_index);
            preg_match('/<td class="td-01 ranktop">[\s\S]*?<\/td>/',$v,$index);
            preg_match('/<span>[\s\S]*?<\/span>/',$title_index[0],$index_number);
            preg_match('/<a[\s\S]*?<\/a>/',$title_index[0],$title);
//            dump($title);
//            dump($index);
//            dump($index_number);
            if(!isset($index[0])){
                $arr[$k]=[
                    'title'=>trim(strip_tags($title[0])),
                    'index'=>trim('热'),
                    'time'=>time(),
                    'index_number'=>'0'
                ];
            }else{
                $arr[$k]=[
                    'title'=>trim(strip_tags($title[0])),
                    'index'=>trim(strip_tags($index[0])),
                    'index_number'=>trim(strip_tags($index_number[0])),
                    'time'=>time()
                ];
            }
        }
        dump($arr);
        DB::table('wb_search')->insert($arr);
    }
    /*
    *get方法
    *
    */
    protected function  sendDateByGet($url,$https=true,$method='get',$cookie=''){
        //初始化
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, "$url");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        return $output;
    }


    /*
    *post方法
    *
    */
    protected  function  sendDateByPost($url,$https=true,$method='post',$jsonStr=null){
        // 初始化curl
        $ch = curl_init($url);
        // 字符串不直接输出，进行一个变量的存储
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // https请求
        if ($https === true) {
            // 确保https请求能够请求成功
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        }
        // post请求
        if ($method == 'post') {
            curl_setopt($ch,CURLOPT_POST,true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
            // 只需要用个 http 头就能传递 json
            //curl_setopt($ch, CURLOPT_HTTPHEADER, $header
            //);
        }
        // 发送请求

        $str = curl_exec($ch);

        $aStatus = curl_getinfo($ch);
        curl_close($ch);

        if(intval($aStatus["http_code"])==200){

            return json_decode($str);
        }else{
            return false;
        }
    }
}
